/*
 * Created by SharpDevelop.
 * User: user
 * Date: 12/14/2008
 * Time: 7:55 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data;
using System.Collections.Generic;

namespace csharp.rad.general
{
	/// <summary>
	/// Description of Status.
	/// </summary>
	public class Status : csharp.common.DefaultClass , csharp.common.IClass
	{
		private String _name;
		private String _create_date;
		private String _table;
		private String _value;
		
		public Status()
		{
			gsql.table = "common.status";
			_sequence  = "common.status_status_id_seq";
		}		
		
		#region properties
			public string name 
			{
				get { return _name; }
				set { _name = value; }
			}
			
			public string createDate 
			{
				get { return _create_date; }
				set { _create_date = value; }
			}
	
			public string table 
			{
				get { return _table; }
				set { _table = value; }
			}
				
			public string value 
			{
				get { return _value; }
				set { _value = value; }
			}
		#endregion
		
		/// <summary>
		/// Clear the object atributes.
		/// </summary>
		public void clear()
		{
			_id = "";
			_name = "";
			_create_date = "";
			_table = "";
			_value = "";
		}			
	
		/// <summary>
		/// Fill the class with data of DataRow
		/// </summary>
		/// <param name="dr">DataRow with the data to fill the class.</param>				
		public override void get(DataRow dr)
		{
			_id          = dr["status_id"].ToString();
			_name        = dr["status_name"].ToString();
			_create_date = Convert.ToDateTime(dr["status_create_date"]).ToString(common.CONSTANTS.dateFormatBrazilian);
			_table       = dr["status_table"].ToString();
			_value       = dr["status_value"].ToString();
		}
		
		public override void setGsql()
		{
			gsql.setValue(_id, "status_id");
			gsql.setValue(_name, "status_name");
			//gsql.setValue(_create_date, "status_create_date");
			gsql.setValue(_table, "status_table");
			gsql.setValue(_value, "status_value");
		}
	}


	public class Statuss : csharp.common.DefaultList, csharp.common.IList
	{
		public Statuss()
		{
			gsql.table= "common.status";
		}
		
		public Status get(Int32 index)
		{
			return (Status)items[index];
		}
		
		public Status get()
		{
			return (Status)items[index];
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.additionalWhere = "ORDER BY status_table";
			
			return mountList();
		}
		
		public bool fillList(String nameTable)
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere= "status_table = '"+nameTable+"'";
			gsql.additionalWhere = "ORDER BY status_name";
			return mountList();
		}
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			tam= gsql.getRowCount();
			items.Clear();
			for (int i=0; i < tam; i++)
			{
				Status s = new Status();
				s.get(gsql.getRow(i));
				items.Add(s);
			}
			return true;
		}
	}
}
